/*
 * Filename:    CalculationKpi.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年10月15日
 */
package com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.biz.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.paic.mhis.hcpms.quota.calculationreport.calculationdataItem.dto.CalculationResultDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.util.AnalyticFormulaUtil;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.util.CalculationFormulaUtil;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.CalculationFormulaVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.common.vo.CalculationResultVo;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.dto.CalculationKpiParmDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.dto.KpiBaseFormulaDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.dto.KpiCalculationResultDTO;
import com.paic.mhis.hcpms.quota.calculationreport.calculationkpi.util.ContextUtil;

public class CalculationKpi {
	private List<CalculationFormulaVo> formulaList = new ArrayList<CalculationFormulaVo>();

	public void setCalculationFormula(List<KpiBaseFormulaDTO> list) {
		for (int i = 0; i < list.size(); i++) {
			KpiBaseFormulaDTO formula = list.get(i);
			if (!"".equals(formula.getFormula()) && formula.getFormula() != null) {
				formulaList.add(AnalyticFormulaUtil.createCalculationFormula(i + 1, formula.getVisitType(),
						formula.getKpiCode(), formula.getFormulaId(), formula.getFormula()));
			}
		}
	}

	/**
	 * 公式计算
	 * 
	 * @param dto
	 * @param dataDTO
	 * @param resultDto
	 * @return
	 */
	public List<KpiCalculationResultDTO> calculationKpi(CalculationKpiParmDTO dto, Map<String, Object> dataDTO,
			CalculationResultDTO resultDto) {
		List<KpiCalculationResultDTO> resultList = new ArrayList<KpiCalculationResultDTO>();
		for (CalculationFormulaVo formulaVo : formulaList) {
			if (ContextUtil.VISIT_TYPE_T.equals(formulaVo.getVisitType())
					|| formulaVo.getVisitType().equals(dto.getVisitType())) {
				KpiCalculationResultDTO resultVo = new KpiCalculationResultDTO();
				resultVo.setSociationCode(dto.getAreaCode());
				resultVo.setPeriod(dto.getPeriod());
				resultVo.setKpiTime(dto.getKpiTime());
				resultVo.setVisitType(dto.getVisitType());
				resultVo.setKpiCode(formulaVo.getKpiCode());
				resultVo.setUseId(dto.getUserId());
				if (ContextUtil.KPI_GRADE.equals(dto.getCalculationType())) {
					resultVo.setGradeCode(dataDTO.get(ContextUtil.PARM_NAME_GRADE).toString());
				}
				if (ContextUtil.KPI_CATEGORY.equals(dto.getCalculationType())) {
					resultVo.setCategoryCode(dataDTO.get(ContextUtil.PARM_NAME_CATEGORY).toString());
				}
				if (ContextUtil.KPI_MEDICAL.equals(dto.getCalculationType())) {
					resultVo.setMedicalCode(dataDTO.get(ContextUtil.PARM_NAME_MEDICAL).toString());
				}
				if (ContextUtil.KPI_DEPART.equals(dto.getCalculationType())) {
					resultVo.setMedicalCode(dataDTO.get(ContextUtil.PARM_NAME_MEDICAL).toString());
					resultVo.setDepartmentCode(dataDTO.get(ContextUtil.PARM_NAME_DEPART).toString());
				}
				if (ContextUtil.KPI_DOCTOR.equals(dto.getCalculationType())) {
					resultVo.setMedicalCode(dataDTO.get(ContextUtil.PARM_NAME_MEDICAL).toString());
					resultVo.setDepartmentCode(dataDTO.get(ContextUtil.PARM_NAME_DEPART).toString());
					resultVo.setDoctorCode(dataDTO.get(ContextUtil.PARM_NAME_DOCTOR).toString());
				}
				// try {
				CalculationResultVo result = CalculationFormulaUtil.calculationFormula(dataDTO, formulaVo);
				resultVo.setKpiValue(result.getResultValue());
				resultVo.setKpiParm1(result.getLeftValue());
				resultVo.setKpiParm2(result.getRightValue());
				resultVo.setIsValid(ContextUtil.STATU_0);
				// } catch (Exception e) {
				// resultVo.setKpiValue(new BigDecimal(0));
				// resultVo.setKpiParm1(new BigDecimal(0));
				// resultVo.setKpiParm2(new BigDecimal(0));
				// resultVo.setIsValid(ContextUtil.STATU_1);
				// }
				resultVo.setUseId(dto.getUserId());
				resultList.add(resultVo);
			}
		}
		return resultList;
	}

	public static void main(String[] args) {
		System.out.println(new BigDecimal("83774713961.91").add(new BigDecimal("83774713961.91")));
	}
}
